package com.kanche.bigbaby.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kanche.bigbaby.AppConfig;
import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.DateUtils;
import com.kanche.bigbaby.collection.StringUtils;
import com.kanche.bigbaby.collection.domain.Vehicle;
import com.kanche.bigbaby.support.utils.ViewUtility;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenshao_Young on 15/5/26.
 */
public class VehicleListAdapter extends BaseAdapter {

    List<Vehicle> list = new ArrayList<>();
    Context context;
    public VehicleListAdapter(Context context,List<Vehicle> list) {
        this.context = context;
        this.list  = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh ;
        if(convertView == null){
            convertView = View.inflate(context, R.layout.adapter_smlistview,null);
            vh = new ViewHolder();
            vh.iv_car = ViewUtility.findViewById(convertView, R.id.iv_car);
            vh.title = ViewUtility.findViewById(convertView,R.id.title);
            vh.price = ViewUtility.findViewById(convertView,R.id.price);
            vh.date = ViewUtility.findViewById(convertView,R.id.date);
            vh.address = ViewUtility.findViewById(convertView,R.id.address);
            vh.time = ViewUtility.findViewById(convertView,R.id.time);

            convertView.setTag(vh);

        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        Vehicle vehicle = list.get(position);
        vh.title.setText(vehicle.getSpec().getDisplayShort());
        int quotedPrice = vehicle.getPrice().getQuotedPrice();
        if (quotedPrice == 0) {
            vh.price.setText("");
        } else {
            vh.price.setText("￥" + vehicle.getPrice().getQuotedPriceByTenGrand() +"w");
        }
        int offerPrice = vehicle.getPrice().getOfferPrice();

        if (offerPrice != 0) {
            vh.price.setText("[售出]￥" + vehicle.getPrice().getOfferPriceByTenGrand() + "w");
        }
        String ct = vehicle.getUpdateAt();
        int mileage = vehicle.getSummary().getMileage();
        if (StringUtils.hasText(ct)) {

            String prettyText = DateUtils.formatPrettyTime(ct);
            vh.date.setText(prettyText);
            if (mileage == 0) {

            } else {
                vh.date.setText(prettyText+"/" + vehicle.getSummary().getMileageByTenKm() + " 万公里");
            }
        }
        vh.address.setText("帝都");


        //
        String path = vehicle.getGallery().getFirstAvailablePhotoPath();
        if(!TextUtils.isEmpty(path)){
            ImageLoader.getInstance().displayImage("file:/"+path,vh.iv_car, AppConfig.defaultImageLoadOptions);
        }

        return convertView;
    }
    class ViewHolder{
        ImageView iv_car;
        TextView title,price,date,address,time;
    }

    public void setList(List<Vehicle> list) {
        this.list = list;
    }
}
